/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "prs_FrmString.h"
using namespace prs;

// TNodeFrmStringControl //////////////////////////////////////////////////////

//
bool TNodeFrmStringControl::UpdateValue(svl::TString V)
{
  if (frm::control::TStringEdit::UpdateValue(V)) {
    node.Value = V;
    return true;
  }
  return false;
}

// TNodeFrmString /////////////////////////////////////////////////////////////

//
TNodeFrmStringControl *TNodeFrmString::BuildControl(
  frm::control::TPanel *ParentControl
)
{
  TNodeFrmStringControl *P = Control;

  if (!P || &P->GetWindow() != &ParentControl->GetWindow()) {
    delete P; 
    P = new TNodeFrmStringControl(ParentControl->GetWindow(),*this);
    Control = P;
    P->SetPanelEvents(controlEvents);
    ParentControl->AddChildLast(P,false);

    P->SetValue(value);
    P->SetEnableEdit( EnEdit.GetValue() );
  }
  
  return P;
}

//
void TNodeFrmString::TNode_SetPropPointerValue(TAttrPointerBase &Attr,svl::TPointer *V) {
  if (&Attr == &Style) { 
    vStyle = dynamic_cast<TNodeStyle*>(V);
    TNodeFrmControl::setControlStyle(this);
    return;
  }
  if (&Attr == &LocalStyle) { 
    vLocalStyle = dynamic_cast<TNodeStyle*>(V);
    TNodeFrmControl::setControlStyle(this);
    return;
  }
  TNodeFrmControl::TNode_SetPropPointerValue(Attr,V);
}

//
void TNodeFrmString::TNode_SetPropStringValue(TAttrStringBase &Attr,svl::TString &V) { 
  if (&Attr == &Value) { 
    value = V;
    updateValueInControl(); 
    return;
  }
  TNodeFrmControl::TNode_SetPropStringValue(Attr,V);
}

//
void TNodeFrmString::TNode_AddPropStringValue(TAttrStringBase &Attr,svl::TString &V) { 
  if (&Attr == &Value) { 
    value << V; 
    updateValueInControl(); 
    return;
  }
  TNodeFrmControl::TNode_AddPropStringValue(Attr,V);
}

//
svl::TString &TNodeFrmString::TNode_GetPropStringValue(TAttrStringBase &Attr) { 
  if (&Attr == &Value) { 
    return value;
  }
  return TNodeFrmControl::TNode_GetPropStringValue(Attr);
}

//
void TNodeFrmString::updateValueInControl() { 
  TNodeFrmStringControl *P = Control; 
  TNodeUseTemplateParam::setValueFromBindAttr(Value,P);

  static svl::TLocksStack::TState St;
  if (svl::TLocksStack::IsTop(St,P)) { 
    return;
  }
  svl::TLocksStack Push(St,P);

  if (P) { 
    frm::control::TPanel::TBeginPaint BeginPaint(P);
    P->frm::control::TStringEdit::UpdateValue(value);
  }
}

// TRuleFrmString /////////////////////////////////////////////////////////////

//
TRuleFrmString::TRuleFrmString(
  snt::text::TContext &Context,
  svl::TVariant &VParentNode,
  TRuleFrmChildrenBase &RFrmChildren,
  TRuleSetNodeIdOptionally &RSetNodeIdOptionally,
  TRuleSetAttrValue &RSetAttrValue,
  TRuleSetStyle &RStyle
) 
:vParentNode(VParentNode)
{
  //snt::TFVoid F;
  TFuncNewNodeIntoAttrOldChildren FNewNode;
  snt::TReturn Return;
  //snt::text::TIdent I(Context);
  snt::text::TSequence T(Context);
  //snt::text::TString S(Context);
  snt::text::TKeyword K(Context);

  this->Name(GetClassName()).Local(vParentNode)
  | K.New("string") 
    & FNewNode.New(vParentNode,TNodeFrmString()) 
    & RSetNodeIdOptionally.New()
    & RAttr.New() 
    & Return.New()
  ;

  RAttr.Name(GetClassName() + "[RAttr]")
  | T.New("(") 
    & RAttr1.New() 
    & RFrmChildren.New() 
    & T.New(")") 
    & Return.New()

  | Return.New()
  ;

  RAttr1.Name(GetClassName() + "[RAttr1]")
  | AAttr1.New()
  ;
  
  AAttr1.Name(GetClassName() + "[AAttr1]")
  | RSetAttrValue.New(
      VParentNode,
      TRuleSetAttrValue::TAttrs()
       .Add(attrName_Value  ,this,&TRuleFrmString::FSetParamString) 
       .Add(attrName_EnEdit ,this,&TRuleFrmString::FSetParamBool)
    )
    & AAttr1.New()
  
  | RStyle.New() & AAttr1.New()
  | Return.New()
  ;
}
